Chapter 14 — Data Visualisation with Matplotlib
Code Reference File — Copy and paste as needed

========================================
14.1.1 Import
========================================
import matplotlib.pyplot as plt

========================================
14.2.1 Simple Line Chart
========================================
plt.plot([1, 2, 3, 4], [10, 20, 25, 30])
plt.show()

========================================
14.2.2 Labels and Title
========================================
plt.plot([1, 2, 3, 4], [10, 20, 25, 30])
plt.xlabel('Month')
plt.ylabel('Sales')
plt.title('Monthly Sales')
plt.show()

========================================
14.2.3 Multi-Series Line Chart
========================================
x = [1, 2, 3, 4, 5]
plt.plot(x, [10,20,25,30,35], color='steelblue', linestyle='--', marker='o', label='Series 1')
plt.plot(x, [5,15,20,28,32],  color='coral',     linestyle='-',  marker='s', label='Series 2')
plt.legend()
plt.tight_layout()
plt.show()

========================================
14.2.4 Revenue Over Time
========================================
monthly_revenue = sales_model.groupby(pd.to_datetime(sales_model['Date']).dt.to_period('M'))['Revenue'].sum()
plt.figure(figsize=(12, 5))
plt.plot(monthly_revenue.index.astype(str), monthly_revenue.values, color='steelblue', linewidth=2)
plt.xlabel('Month')
plt.ylabel('Total Revenue')
plt.title('Monthly Revenue Over Time')
plt.xticks(rotation=45, ha='right')
plt.tight_layout()
plt.show()

========================================
14.3.1 Vertical Bar Chart
========================================
revenue_by_line = sales_model.groupby('Product Line')['Revenue'].sum().sort_values(ascending=False)
plt.figure(figsize=(10, 6))
plt.bar(revenue_by_line.index, revenue_by_line.values, color='steelblue')
plt.xlabel('Product Line')
plt.ylabel('Total Revenue')
plt.title('Total Revenue by Product Line')
plt.xticks(rotation=45, ha='right')
plt.tight_layout()
plt.show()

========================================
14.3.2 Horizontal Bar Chart
========================================
revenue_by_retailer = sales_model.groupby('Retailer Type')['Revenue'].sum().sort_values()
plt.figure(figsize=(10, 6))
plt.barh(revenue_by_retailer.index, revenue_by_retailer.values, color='coral')
plt.xlabel('Total Revenue')
plt.title('Total Revenue by Retailer Type')
plt.tight_layout()
plt.show()

========================================
14.4.1 Scatter Plot
========================================
plt.figure(figsize=(8, 6))
plt.scatter(sales_model['Sale Price'], sales_model['Quantity Sold'], alpha=0.3, color='steelblue')
plt.xlabel('Sale Price')
plt.ylabel('Quantity Sold')
plt.title('Sale Price vs Quantity Sold')
plt.tight_layout()
plt.show()

========================================
14.5.1 Histogram
========================================
plt.figure(figsize=(8, 5))
plt.hist(sales_model['Sale Price'], bins=30, color='steelblue', edgecolor='white')
plt.xlabel('Sale Price')
plt.ylabel('Frequency')
plt.title('Distribution of Sale Price')
plt.tight_layout()
plt.show()

========================================
14.6.1 Subplots
========================================
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(14, 5))
revenue_by_line = sales_model.groupby('Product Line')['Revenue'].sum().sort_values(ascending=False)
ax1.bar(revenue_by_line.index, revenue_by_line.values, color='steelblue')
ax1.set_title('Revenue by Product Line')
ax1.tick_params(axis='x', rotation=45)
revenue_by_retailer = sales_model.groupby('Retailer Type')['Revenue'].sum().sort_values()
ax2.barh(revenue_by_retailer.index, revenue_by_retailer.values, color='coral')
ax2.set_title('Revenue by Retailer Type')
plt.tight_layout()
plt.show()

========================================
14.7.1 Save as PNG
========================================
plt.savefig('revenue_by_product_line.png', dpi=150, bbox_inches='tight')

========================================
14.7.2 Download from Colab
========================================
from google.colab import files
files.download('revenue_by_product_line.png')
